package com.fudan.backend.service.impl;

import com.fudan.backend.dao.StoreDao;
import com.fudan.backend.dao.StoreModifyRequestDao;
import com.fudan.backend.exception.BaseException;
import com.fudan.backend.exception.RetCode;
import com.fudan.backend.pojo.Store;
import com.fudan.backend.pojo.StoreModifyRequest;
import com.fudan.backend.pojo.statics.RequestStatus;
import com.fudan.backend.service.StoreModifyRequestService;
import com.fudan.backend.util.Snowflake;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

@Service
public class StoreModifyRequestServiceImpl implements StoreModifyRequestService {

    @Autowired
    StoreModifyRequestDao storeModifyRequestDao;

    @Autowired
    StoreDao storeDao;

    @Autowired
    Snowflake snowflake;

    @Override
    public RetCode addStoreModifyRequest(StoreModifyRequest storeModifyRequest) {

        Date date = new Date();

        storeModifyRequest.setStoreModifyRequestId(((Long) snowflake.nextId()).toString());
        storeModifyRequest.setInitiateTime(date);
        storeModifyRequest.setProcessTime(null);
        storeModifyRequest.setStatus(RequestStatus.APPLYING);

        int i = storeModifyRequestDao.insertStoreModifyRequest(storeModifyRequest);
        if(i == 1){
            return RetCode.STORE_MODIFY_REQUEST_SUCCESS;
        }else {
            return RetCode.STORE_MODIFY_REQUEST_INSERT_FAILURE;
        }
    }

    @Override
    @Transactional
    public RetCode processStoreModifyRequest(StoreModifyRequest storeModifyRequest) {
        Date date = new Date();
        storeModifyRequest.setProcessTime(date);

        int updateStoreModifyRequest = storeModifyRequestDao.updateStoreModifyRequest(storeModifyRequest);
        if(updateStoreModifyRequest == 0){
            throw new BaseException(RetCode.STORE_MODIFY_REQUEST_UPDATE_FAILURE.getCode(),RetCode.STORE_MODIFY_REQUEST_UPDATE_FAILURE.getMessage());
        }

        Integer status = storeModifyRequest.getStatus();
        String storeId = storeModifyRequest.getStoreId();

        if(status == RequestStatus.APPROVED){
            Store store = storeDao.selectStoreByStoreId(storeId);
            store.setStoreName(storeModifyRequest.getStoreName());
            store.setCategory(storeModifyRequest.getCategory());
            store.setStoreProfile(storeModifyRequest.getStoreProfile());
            store.setFilingAddress(storeModifyRequest.getFilingAddress());
            store.setPictures(storeModifyRequest.getPictures());
            store.setStatus(storeModifyRequest.getStatus());
            int updateStore = storeDao.updateStore(store);
            if(updateStore == 0){
                throw new BaseException(RetCode.STORE_UPDATE_FAILURE.getCode(),RetCode.STORE_UPDATE_FAILURE.getMessage());
            }
        }
        return RetCode.STORE_MODIFY_REQUEST_SUCCESS;
    }

    @Override
    public PageInfo<StoreModifyRequest> getAllStoreModifyRequest(int current, int pageSize) {
        PageHelper.startPage(current, pageSize);
        List<StoreModifyRequest> storeModifyRequests = storeModifyRequestDao.selectAllStoreModifyRequest();
        return new PageInfo<StoreModifyRequest>(storeModifyRequests, 5);
    }

    @Override
    public StoreModifyRequest getStoreModifyRequestByStoreModifyRequestId(String storeModifyRequestId) {
        StoreModifyRequest storeModifyRequest = storeModifyRequestDao.selectStoreModifyRequestByStoreModifyRequestId(storeModifyRequestId);
        if(storeModifyRequest == null){
            throw new BaseException(RetCode.STORE_MODIFY_REQUEST_NOT_FOUND.getCode(), RetCode.STORE_MODIFY_REQUEST_NOT_FOUND.getMessage());
        }
        return storeModifyRequest;
    }
}
